package net.ojbk.saishi.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class ExcelTool {
	public static void main(String[] args) {
		String path = "D:\\java\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\saishi_shiyi_ojbk\\MATCH-DATA\\76670\\XOSQCU\\StatisticalInfo.xls";
		String[] headers = {"1", "2", "3", "4"};
		try {
			ExcelTool.createExcelOfStudents("1aaa", headers, path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
    public static void createExcelOfStudents(String tableName, String[] headers,String path) throws Exception{  
        
        //创建工作表 
    	Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("信息"); 
        //显示标题  
        Row title_row = sheet.createRow(0);  
        title_row.setHeight((short)(40*20));  
          
        Cell title_cell = title_row.createCell(0);   
           
          
        Row header_row = sheet.createRow(1);  
        header_row.setHeight((short)(20*24));  
          
        //创建单元格的 显示样式  
        CellStyle style = wb.createCellStyle();  
        style.setAlignment(CellStyle.ALIGN_CENTER); //水平方向上的对其方式  
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);  //垂直方向上的对其方式  
          
          
        title_cell.setCellStyle(style);  
        title_cell.setCellValue("参赛信息");  
          
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,headers.length-1));  //合并单元格 起始行 末尾行  起始列  末尾列
          
         
        for(int i=0;i<headers.length;i++){  
            //设置列宽   基数为256  
            sheet.setColumnWidth(i, 30*256);  
            Cell cell = header_row.createCell(i);  
            //应用样式到  单元格上  
            cell.setCellStyle(style);  
            cell.setCellValue(headers[i]);  
        }  
        File pathFile = new File (path);
        if (pathFile.exists())
        	pathFile.delete();
        FileOutputStream fileOut = new FileOutputStream(path);  
        wb.write(fileOut);  
        fileOut.close();  
    } 
    
    public static void addOneData(Object [][] data,String path) throws IOException {
    	FileInputStream fs=new FileInputStream(path);  //获取d://test.xls  
        POIFSFileSystem ps=new POIFSFileSystem(fs);  //使用POI提供的方法得到excel的信息  
        HSSFWorkbook wb=new HSSFWorkbook(ps);    
        HSSFSheet sheet=wb.getSheetAt(0);  //获取到工作表，因为一个excel可能有多个工作表  
        HSSFRow row=sheet.getRow(1);  //获取第一行（excel中的行默认从0开始，所以这就是为什么，一个excel必须有字段列头），即，字段列头，便于赋值
        System.out.println(sheet.getLastRowNum()+"aa"+row.getLastCellNum());
        FileOutputStream fileOut = new FileOutputStream(path);
        int height=sheet.getLastRowNum();
        for(int i=0;i<data.length;i++){  
            
            row = sheet.createRow(height+i+1);  
            row.setHeight((short)(20*20)); //设置行高  基数为20  
            for(int j=0;j<data[i].length;j++){
            	row.createCell(j).setCellValue(data[i][j].toString()); 
            }  
              
        } 
        fileOut.flush();  
        wb.write(fileOut);  
        fileOut.close(); 
    }
}
